<?php
declare(strict_types=1);

namespace App\Service;
use App\Constants\XZXResponseCode;
use App\Exception\XZXValidationException;
use App\Model\Dao\SysAnnouncementDao;
use App\Model\Dao\SysAnnouncementReadDao;
use App\Model\Dao\SysAnnouncementRoleRelationDao;
use App\Utils\CustomUtils;
use App\Utils\DateUtils;
use Hyperf\Di\Annotation\Inject;

class SysAnnouncementService
{
    /**
     * @Inject()
     * @var SysAnnouncementDao
     */
    private $SysAnnouncementDao;

    /**
     * @Inject()
     * @var SysAnnouncementReadDao
     */
    private $SysAnnouncementReadDao;

    /**
     * @Inject()
     * @var SysAnnouncementRoleRelationDao
     */
    private $SysAnnouncementRoleRelationDao;

    public function get($param)
    {
        $result = $this->SysAnnouncementDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        $roleRelate = $this->SysAnnouncementRoleRelationDao->getWhereList(['sysAnnouncementId' => $param['id']]);
        (isset($result['createTime']) && !empty($result['createTime'])) && $result['createTime'] = DateUtils::getDateTime($result['createTime']);
        (isset($result['startTime']) && !empty($result['startTime'])) && $result['startTime'] = DateUtils::getDateTime($result['startTime']);
        (isset($result['endTime']) && !empty($result['endTime'])) && $result['endTime'] = DateUtils::getDateTime($result['endTime']);
        if (!empty($roleRelate)) {
            $result['roleId'] = array_column($roleRelate, "roleId");
        } else {
            $result['roleId'] = [];
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

    public function del($param)
    {
        $result = $this->SysAnnouncementDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        
        try {
            $this->SysAnnouncementDao->del($param['id'],false);
        } catch (\Throwable $th) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DEL_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function add($param)
    {
        $roleId = isset($param['roleId']) ? $param['roleId'] : [];
        unset($param['roleId']);
        unset($param['seveTime']);
        $data = $param;
        $data['id'] = CustomUtils::createUUID();
        $data['updateBy']   = $param['createBy'];
        $data['createTime'] = time();

        $data['startTime'] = strtotime($data['startTime']);
        $data['endTime'] = strtotime($data['endTime']);
        
        try {
            $this->SysAnnouncementDao->insert($data);

            if (!empty($roleId) && is_array($roleId)) {
                foreach ($roleId as $value) {
                    $relate = [
                        'id'                => CustomUtils::createUUID(),
                        'sysAnnouncementId' => $data['id'],
                        'roleId'            => $value,
                        'createTime'        => time()
                    ];
                    $this->SysAnnouncementRoleRelationDao->add($relate);
                }
            }
        } catch (\Throwable $th) {
            CustomUtils::xzxDebug($th);
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::ADD_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function edit($param)
    {
        $info = $this->SysAnnouncementDao->get($param['id']);
        if (!$info) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }

        $roleId = isset($param['roleId']) ? $param['roleId'] : [];
        $id = $param['id'];
        $data = array();
        $data['title'] = $param['title'];
        $data['content'] = $param['content'];
        $data['startTime'] = strtotime($param['startTime']);
        $data['endTime'] = strtotime($param['endTime']);

        try {
            $this->SysAnnouncementDao->update($id, $data);

            if (!empty($roleId) && is_array($roleId)) {
                $this->SysAnnouncementRoleRelationDao->delWhere(['sysAnnouncementId' => $id]);
                foreach ($roleId as $value) {
                    $relate = [
                        'id'                => CustomUtils::createUUID(),
                        'sysAnnouncementId' => $param['id'],
                        'roleId'            => $value,
                        'createTime'        => time()
                    ];
                    $this->SysAnnouncementRoleRelationDao->add($relate);
                }
            }
        } catch (\Throwable $th) {
            CustomUtils::xzxDebug($th->getMessage());
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::EDIT_FAIL));
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),CustomUtils::xzxEmptyObject());
    }

    public function list(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];        

        $total = $this->SysAnnouncementDao->getManageCount($param);
        $list = $this->SysAnnouncementDao->getManageList($param, $page, $limit);

        foreach ($list as $k => $v) {
            (isset($v['createTime']) && !empty($v['createTime'])) && $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            (isset($v['startTime']) && !empty($v['startTime'])) && $v['startTime'] = DateUtils::getDateTime($v['startTime']);
            (isset($v['endTime']) && !empty($v['endTime'])) && $v['endTime'] = DateUtils::getDateTime($v['endTime']);
            $list[$k] = $v;
        }

        $data = array(
            'total' => $total,
            'list' => $list
        );

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }

    public function listForIndex(array $param)
    {

        $limit = $param['limit'];
        $page = $param['page'];
        $param['isRead'] = 0;
        $read = $this->SysAnnouncementDao->getCountForMch($param);
        unset($param['uid']);
        unset($param['isRead']);
        $total = $this->SysAnnouncementDao->getCountForMch($param);
        $list = $this->SysAnnouncementDao->getListForMch($param, $page, $limit);

        foreach ($list as $k => $v) {

            (isset($v['createTime']) && !empty($v['createTime'])) && $v['createTime'] = DateUtils::getDateTime($v['createTime']);
            (isset($v['startTime']) && !empty($v['startTime'])) && $v['startTime'] = DateUtils::getDateTime($v['startTime']);
            (isset($v['endTime']) && !empty($v['endTime'])) && $v['endTime'] = DateUtils::getDateTime($v['endTime']);

            $list[$k] = $v;
        }

        $data = array(
            'notRead' => $total == 0 ? 0 : $total - $read,
            'total' => $total,
            'list' => $list
        );

        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$data);
    }

    public function read($param) {
        $isRead = $this->SysAnnouncementReadDao->getByAnnouncementIdAndAdminIdAndSource($param['uid'], $param['sysAnnouncementId'], $param['source']);
        if ($isRead) {
            return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), "");
        }
        $isRead = [
            'id'    => CustomUtils::createUUID(),
            'sysAnnouncementId' => $param['sysAnnouncementId'],
            'adminId'           => $param['uid'],
            'source'            => $param['source'],
            'createTime'        => time(),
        ];
        $this->SysAnnouncementReadDao->insert($isRead);
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE), "");
    }

    public function getForMch($param) {
        $result = $this->SysAnnouncementDao->get($param['id']);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        (isset($result['createTime']) && !empty($result['createTime'])) && $result['createTime'] = DateUtils::getDateTime($result['createTime']);
        (isset($result['startTime']) && !empty($result['startTime'])) && $result['startTime'] = DateUtils::getDateTime($result['startTime']);
        (isset($result['endTime']) && !empty($result['endTime'])) && $result['endTime'] = DateUtils::getDateTime($result['endTime']);

        $isRead = $this->SysAnnouncementReadDao->getWhere(['adminId' => $param['uid'], 'sysAnnouncementId' => $param['id']]);
        if ($isRead) {
            $result['isRead'] = 1;
        } else {
            $result['isRead'] = 0;
        }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }


    public function getBankAccount($param) {
        $queryParam = array(
            'type' => 3,
        );

        $result = $this->SysAnnouncementDao->getWhere($queryParam);
        if (!$result) {
            throw new XZXValidationException(XZXResponseCode::getMessage(XZXResponseCode::DATA_NOT_FOUND));
        }
        (isset($result['createTime']) && !empty($result['createTime'])) && $result['createTime'] = DateUtils::getDateTime($result['createTime']);
        (isset($result['startTime']) && !empty($result['startTime'])) && $result['startTime'] = DateUtils::getDateTime($result['startTime']);
        (isset($result['endTime']) && !empty($result['endTime'])) && $result['endTime'] = DateUtils::getDateTime($result['endTime']);

        // $isRead = $this->SysAnnouncementReadDao->getWhere(['adminId' => $param['uid'], 'sysAnnouncementId' => $param['id']]);
        // if ($isRead) {
        //     $result['isRead'] = 1;
        // } else {
        //     $result['isRead'] = 0;
        // }
        return CustomUtils::xeServiceReturnData(XZXResponseCode::SUCCESS_CODE,XZXResponseCode::getMessage(XZXResponseCode::SUCCESS_CODE),$result);
    }

}